﻿/***************************************************************
 * Name:      HouseholdManagementApp.cpp
 * Purpose:   Code for Application Class
 * Author:    JHC (wqvbjhc@gmail.com)
 * Created:   2012-08-30
 * Copyright: JHC (http://my.csdn.net/wqvbjhc)
 * License:
 **************************************************************/

#include "HouseholdManagementApp.h"
#include "PublicData.h"
#include <wx/fileconf.h>
#include <wx/wfstream.h>
#include "DlgLogin.h"

//(*AppHeaders
#include "HouseholdManagementMain.h"
#include <wx/image.h>
//*)
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

IMPLEMENT_APP(HouseholdManagementApp);

Configure app_conf_;//全局配置参数

bool HouseholdManagementApp::OnInit()
{
#if defined(__WXDEBUG__) && !defined(__VISUALC__)
    wxFrame* pLogFrame;
    wxLogWindow* m_pLogWindow = new wxLogWindow(NULL, wxT("Log") );
    pLogFrame = m_pLogWindow->GetFrame();
    pLogFrame->SetWindowStyle(wxDEFAULT_FRAME_STYLE|wxSTAY_ON_TOP);
    pLogFrame->SetSize( wxRect(0,450,400,250) );
    wxLog::SetActiveTarget(m_pLogWindow);
#endif
    while(!ReadConfFile(app_conf_)) {
        wxLogDebug(_T("w db=%s\r\nbg=%s\r\nneedlog=%s"),app_conf_.dbFilePath,app_conf_.bgPicPath,app_conf_.needlogin);
        CreateDefConfFile();
    }
    wxLogDebug(_T("db=%s\r\nbg=%s\r\nneedlog=%s"),app_conf_.dbFilePath,app_conf_.bgPicPath,app_conf_.needlogin);
    //初始化sqlite
    if(SQLITE_OK != sqlite3_config(SQLITE_CONFIG_SERIALIZED)) {
        wxLogDebug("sqlite3_config error.");
    }
    if(SQLITE_OK != sqlite3_initialize()) {
        wxLogDebug("sqlite3_initialize error.");
    }
    wxLogDebug(_T("sqlite db is %s"),app_conf_.dbFilePath);
    bool isopen=open_db(app_conf_.dbFilePath,&g_db);
    if(!isopen) {
        wxLogDebug(_T("Open database %s error."),app_conf_.dbFilePath);
    }
    if(app_conf_.needlogin.CmpNoCase(_T("0"))!=0) {
        CDlgLogin loginDlg(NULL);
        if(loginDlg.ShowModal()!=wxID_OK) {
            loginDlg.Destroy();
            return false;//exit the application immediately
        } else {
            loginDlg.Destroy();
        }
    }
    bool wxsOK = true;
    wxInitAllImageHandlers();
    if ( wxsOK )
    {
    	HouseholdManagementFrame* Frame = new HouseholdManagementFrame(0);
    	Frame->Show(true);
    	Frame->Maximize();
    	SetTopWindow(Frame);
    }
    //(*AppInitialize
    //*)
    return wxsOK;
}
int HouseholdManagementApp::OnExit()
{
    close_db(&g_db);
    WriteConfFile(app_conf_);
    return wxApp::OnExit();
}

wxString GetWorkDirectory()
{
    static bool isGet=false;
    static wxString strAppPath;
    if(isGet) {
        return strAppPath;
    }
    wxStandardPathsBase& stdp = wxStandardPaths::Get();
    wxFileName exeFile(stdp.GetExecutablePath());
    strAppPath = exeFile.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
    isGet=true;
    return strAppPath ;
}
///配置信息的读写操作
void CreateDefConfFile()
{
    if(wxFileName::FileExists(CONFIGNAME)==true) {
        return;
    }

    wxString dbfilepath=GetWorkDirectory()+g_DBFILENAME;
    wxFileConfig *pFileConfig = new wxFileConfig(wxTheApp->GetAppName(),_T("Phata"));
    pFileConfig->SetPath(_T("AppConf"));
    pFileConfig->Write(wxT("DbFilePath"),dbfilepath);//给文本配置信息赋值
    pFileConfig->Write(wxT("BgPicPath"),wxEmptyString);//给文本配置信息赋值
    wxLogDebug(_T("path %s"),dbfilepath);
    wxFileOutputStream ConfigOutStream(CONFIGNAME);// 打开需要保存的配置文件
    pFileConfig->Save(ConfigOutStream, DefaultConv);//将配置信息写入文件
    ConfigOutStream.Close();
    delete pFileConfig;
}
bool ReadConfFile(Configure& conf)
{
    if(wxFileName::FileExists(CONFIGNAME)==false) {
        return false;
    }

    wxFileInputStream ConfigInStream(CONFIGNAME);//打开配置文件
    wxFileConfig *pFileConfig = new wxFileConfig(wxEmptyString, wxEmptyString,CONFIGNAME,
            wxEmptyString,wxCONFIG_USE_RELATIVE_PATH);
    pFileConfig->Read(wxT("AppConf/DbFilePath"),&conf.dbFilePath);
    pFileConfig->Read(wxT("AppConf/BgPicPath"),&conf.bgPicPath);
    pFileConfig->Read(wxT("AppConf/NeedLogin"),&conf.needlogin);//只读不写
    delete pFileConfig;
    return true;
}
bool WriteConfFile(const Configure& conf)
{
    if(conf.needlogin.CmpNoCase(_T("0"))!=0) {
        wxFileConfig *pFileConfig = new wxFileConfig(wxEmptyString, wxEmptyString,CONFIGNAME,
                wxEmptyString,wxCONFIG_USE_RELATIVE_PATH);
        pFileConfig->SetPath(_T("AppConf"));
        pFileConfig->Write(wxT("DbFilePath"),conf.dbFilePath);
        pFileConfig->Write(wxT("BgPicPath"),conf.bgPicPath);
        wxFileOutputStream ConfigOutStream(CONFIGNAME);// 打开需要保存的配置文件
        pFileConfig->Save(ConfigOutStream, DefaultConv);//将配置信息写入文件
        ConfigOutStream.Close();
        delete pFileConfig;
    }
    return true;
}
